package com.zlfund.cat.zjgsmk.ui.home.smk_activity;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.adapter.AdapterFactory;
import com.zlfund.cat.zjgsmk.adapter.BaseBean;
import com.zlfund.cat.zjgsmk.adapter.LoadMoreBean;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewAdapter;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewHolder;
import com.zlfund.cat.zjgsmk.adapter.RecyclerViewMulTypeAdapterHelper;
import com.zlfund.cat.zjgsmk.adapter.Type;
import com.zlfund.cat.zjgsmk.api.ZjgsmkService;
import com.zlfund.cat.zjgsmk.bean.ActivityBean;
import com.zlfund.cat.zjgsmk.net.INetCallback;
import com.zlfund.cat.zjgsmk.net.RetrofitFiledRequest;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.Logger;
import com.zlfund.cat.zjgsmk.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainActivityFragment extends Fragment {


    @BindView(R.id.rv_activity)
    RecyclerView mRvActivity;

    private AdapterFactory mFactory;
    private Context mContext;
    private RecyclerViewMulTypeAdapterHelper<ActivityBean.RBean> mHelper;
    private RecyclerViewAdapter<ActivityBean.RBean> mAdapter;

    public MainActivityFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_activity, container, false);
        ButterKnife.bind(this, view);
        mFactory = AdapterFactory.singleTon();
        mList = new ArrayList<>();
        mHelper = new RecyclerViewMulTypeAdapterHelper<ActivityBean.RBean>() {
            @Override
            public int getLayoutId(int type) {
                return type == Type.TYPE0 ? R.layout.main_activity_first_item : R.layout.main_activity_other_item;
            }

            @Override
            public void initViews(RecyclerViewHolder holder, int position, ActivityBean.RBean bean) {
                if (holder.getType() == Type.TYPE1) {
                    ImageView iv = holder.getView(R.id.iv_content, ImageView.class);
                    GlideUtil.loadUrl(mContext, bean.getPicture(), iv);
                }
            }

            @Override
            public int getItemViewType(int position) {
                return position == 0 ? Type.TYPE0 : Type.TYPE1;
            }
        };
        mAdapter = mFactory.createMulTypeRvAdapter(mContext, mHelper);
        final LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRvActivity.setLayoutManager(manager);
        mRvActivity.setAdapter(mAdapter);
        initData(mPage);
        mRvActivity.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!mHasMore) return;
                if (mIsLoading) return;
                int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition == mList.size()) {
                    initData(mPage);
                }
            }
        });
        return view;
    }

    private List<BaseBean> mList;
    private int mPage;
    private boolean mHasMore = true;
    private boolean mIsLoading;

    private void initData(int page) {
        mIsLoading = true;
        new RetrofitFiledRequest<ActivityBean>(String.valueOf(page))
                .build(ZjgsmkService.ActivityInfoService.class)
                .enqueueWithRx("getActivityInfo", new INetCallback<ActivityBean>() {
                    @Override
                    public void response(ActivityBean activityBean) {
                        mIsLoading = false;
                        if (activityBean.getR().size() == 0) {
                            error("已经到底啦");
                        } else {
                            mPage++;
                            List<ActivityBean.RBean> list = new ArrayList<>();
                            if (mAdapter.getItemCount() == 0) {
                                list.add(new ActivityBean.RBean());
                            }
                            list.addAll((activityBean).getR());
                            mList.addAll(list);
                            mAdapter.addMoreList(list, true,R.layout.load_more);
                        }
                    }

                    @Override
                    public void error(String str) {
                        mIsLoading = false;
                        mHasMore = false;
                        mAdapter.clearLoadMore();
                        ToastUtil.show(str);
                    }
                });
    }

}
